package com.herui.iot.ui;

import android.view.View;

import com.herui.sdyu_lib.adapter.recycle_view.CommonAdapter;
import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.adapter.recycle_view.wrapper.HeaderAndFooterWrapper;
import com.herui.sdyu_lib.adapter.recycle_view.wrapper.LoadMoreWrapper;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.R;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

/**
 * Created by amanda.shan on 2022/12/4.
 */
public abstract class BaseListSportFragment<T> extends BaseSportFragment {

    protected int pageSize = 10;

    private CommonAdapter<T> mAdapter;
    private List<T> mDatas = new ArrayList<>();

    private HeaderAndFooterWrapper mHeaderAndFooterWrapper;
    private LoadMoreWrapper mLoadMoreWrapper;

    private SwipeRefreshLayout swipeRefreshLayout;

    /**
     * 初始化列表
     * @param recyclerView  列表对象
     * @param itemId        列表每个item的不觉文件
     * @param more          是否可以加载更多
     * @param onItemClickListener   item点击事件 没有可以传null
     */
    protected void initList(RecyclerView recyclerView, int itemId, boolean more,
                          CommonAdapter.OnItemClickListener onItemClickListener) {
        recyclerView.setLayoutManager(new LinearLayoutManager(baseActivity));

        // 单个布局
        mAdapter = new CommonAdapter<T>(baseActivity, itemId, mDatas) {

            @Override
            public void convert(ViewHolder holder, T t, int position) {
                BaseListSportFragment.this.convert(holder, t, position);
            }
        };

        if (!more) {
            recyclerView.setAdapter(mAdapter);
        } else {
// 添加头尾view
            mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);

            mLoadMoreWrapper = new LoadMoreWrapper(mHeaderAndFooterWrapper) {
                @Override
                public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                }
            };
            mLoadMoreWrapper.setLoadMoreView(R.layout.list_loading);
            mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {

                    LogUtils.i("onLoadMoreRequested");
                    doRefresh();

                }

                @Override
                public void onLoadFail() {
                    LogUtils.i("onLoadFail");
                    mLoadMoreWrapper.getLoadViewHolder().getConvertView().setVisibility(View.VISIBLE);
                    mLoadMoreWrapper.getLoadViewHolder().setText(R.id.loading_text, "重新加载");
                    mLoadMoreWrapper.getLoadViewHolder().getView(R.id.progress_wait)
                            .setVisibility(View.GONE);
                }

                @Override
                public void onLoadAll() {
                    LogUtils.i("onLoadAll");
                    mLoadMoreWrapper.getLoadViewHolder().getConvertView().setVisibility(View.GONE);
                }

                @Override
                public void loading() {
                    LogUtils.i("loading");
                    mLoadMoreWrapper.getLoadViewHolder().getConvertView().setVisibility(View.VISIBLE);
                    mLoadMoreWrapper.getLoadViewHolder().setText(R.id.loading_text, "正在加载...");
                    mLoadMoreWrapper.getLoadViewHolder().getView(R.id.progress_wait)
                            .setVisibility(View.VISIBLE);
                }
            });

            recyclerView.setAdapter(mLoadMoreWrapper);
        }
        if (onItemClickListener!=null){
            mAdapter.setOnItemClickListener(onItemClickListener);
        }
    }

    /**
     * 使能下拉加载
     * @param swipeRefreshLayout
     */
    protected void enablePullRefresh(SwipeRefreshLayout swipeRefreshLayout){
        this.swipeRefreshLayout = swipeRefreshLayout;
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout
                .OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    public void refresh(){
        swipeRefreshLayout.setRefreshing(true);
        notifyDataSetChanged();
        mDatas.clear();
        notifyDataSetChanged();
        doRefresh();
    }

    public List<T> getmDatas() {
        return mDatas;
    }

    protected void clearListData(){
        notifyDataSetChanged();
        mDatas.clear();
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     * @param list
     */
    protected void addData(List<T> list){
        notifyDataSetChanged();
        mDatas.addAll(list);
        dataProcess(mDatas);
        notifyDataSetChanged();

        if (mLoadMoreWrapper != null) {
            mLoadMoreWrapper.setLoadState(LoadMoreWrapper.LOAD_SUCESS);
            if (mDatas.size() < pageSize) {
                mLoadMoreWrapper.setLoadState(LoadMoreWrapper.LOAD_ALL);
            }
        }

        if (swipeRefreshLayout != null){
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    /**
     * 数据处理
     * @param datas
     */
    protected void dataProcess(List<T> datas){

    }

    /**
     * 加载数据失败
     */
    protected void loadMoreFail(){
        if (swipeRefreshLayout != null){
            swipeRefreshLayout.setRefreshing(false);
        }
        if (mLoadMoreWrapper!= null){
            mLoadMoreWrapper.setLoadState(LoadMoreWrapper.LOAD_FAIL);
        }
    }

    // 获取页码编号
    protected int getPageNumb(){
        return mDatas.size()/pageSize+1;
    }

    private void notifyDataSetChanged(){
        if (mLoadMoreWrapper == null){
            mAdapter.notifyDataSetChanged();
        }else{
            mLoadMoreWrapper.notifyDataSetChanged();
        }
    }

    protected abstract void convert(ViewHolder holder, T value, final int position);
    protected abstract void doRefresh();
}
